if '__main__' == __name__:
    from python_ai.category.redis.conn.local_redis import r as redis
    from python_ai.common.read_data.redis_numpy import toRedisNdLg, fromRedisNdLg
    from tf2x_cifar10_to_redis import KEY_PREFIX, UINT8_SUFFIX, FLT32_SUFFIX
    import matplotlib.pyplot as plt
    import os
    import pickle
    from python_ai.common.xcommon import *
    import datetime
    from python_basic.study.python_cookbook.ch9_metaprog.x91_timethis import timethis

    sep('names')
    names_list = fromRedisNdLg(redis, KEY_PREFIX + 'names')
    print(names_list)

    for dtype in ['train', 'val', 'test']:
        sep(dtype)
        x_train_flt32, x_train, y_train = None, None, None

        @timethis
        def load():
            global x_train_flt32, x_train, y_train
            print(f'Loading {dtype}')
            t1 = datetime.datetime.now()
            x_train_flt32 = fromRedisNdLg(redis, KEY_PREFIX + 'x_' + dtype + FLT32_SUFFIX)
            x_train = fromRedisNdLg(redis, KEY_PREFIX + 'x_' + dtype + UINT8_SUFFIX)
            y_train = fromRedisNdLg(redis, KEY_PREFIX + 'y_' + dtype)
            t2 = datetime.datetime.now()
            print(f'Loaded. ({t2 - t1})')

        load()
        print('x_' + dtype, x_train.dtype, x_train.shape)
        print('y_' + dtype, y_train.dtype, y_train.shape)

        sep('check flt32')
        print(x_train[0, 0, :10, :])
        print(x_train_flt32[0, 0, :10, :])

        print('Plotting ... (Check and close the plotting window to continue.)')
        plt.figure(figsize=[14, 7])
        spn = 0
        spr = 4
        spc = 8
        for i in range(spr * spc // 2):
            idx = -(i + 1)
            print(idx)
            title = names_list[y_train[idx][0]]
            spn += 1
            plt.subplot(spr, spc, spn)
            plt.title(title + '_uint8')
            plt.imshow(x_train[idx])
            plt.axis('off')
            spn += 1
            plt.subplot(spr, spc, spn)
            plt.title(title + '_flt32')
            plt.imshow(x_train_flt32[idx])
            plt.axis('off')
        plt.show()
        print('Over')
